package com.example.runlian.controller;

import com.example.runlian.pojo.*;
import com.example.runlian.service.DesInfoService;
import com.example.runlian.service.MainTitleService;
import com.example.runlian.service.PictureService;
import com.example.runlian.service.SecondTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Controller
@RequestMapping("manage")
public class IndexJsonManageController {
    @Autowired
    @Qualifier("defaultInfo")
    private DefaultInfo defaultInfo;
    @Autowired
    @Qualifier("mainTitleServiceImpl")
    private MainTitleService mainTitleService;
    @Autowired
    private PageInfo pageInfo;
    @Autowired
    private SecondTitleService secondTitleService;
    @Autowired
    private DesInfoService desInfoService;
    @Autowired
    private PictureService pictureService;
    @Autowired
    private Picture picture;


    /**
     * 分页查询 主标题表,
     * 将查询信息返回到前端页面
     * @param pageInfo
     * @return
     */
    @ResponseBody
    @RequestMapping("getmaintitles")
    public List<MainTitle> getMainTitles(PageInfo pageInfo) {
        return mainTitleService.queryMainTitlesByPage(pageInfo);
    }

    /**
     * 获取 主标题表 的总数据条数,用于分页
     * @return
     */
    @ResponseBody
    @RequestMapping("getmaintitleallpage")
    public Integer getMainTitleAllPage(){
        return mainTitleService.queryMainTitleAllNum();
    }

    /**
     * 更新 主标题 根据mid值
     */
    @ResponseBody
    @RequestMapping("updatemaintitle")
    public Integer updateMainTitleInfo(MainTitle mainTitle){
        if("".equals(mainTitle.getMaTitle()))
            mainTitle.setMaTitle(null);
        return mainTitleService.updateMainTitleByMid(mainTitle);
    }

    /**
     * 分页获取 副标题
     */
    @ResponseBody
    @RequestMapping("getsecondtitles")
    public List<SecondTitle> getSecondTitles(PageInfo pageInfo){
        return secondTitleService.getSecondTitleByPage(pageInfo);
    }

    /**
     * 返回副标题 一共有多少条
     */
    @ResponseBody
    @RequestMapping("getsecondtitlesnum")
    public Integer getSecondTitlesNum(){
        return secondTitleService.querySecondTitleNum();
    }

    /**
     * 修改 副标题信息
     */
    @ResponseBody
    @RequestMapping("updatesecondtitle")
    public Integer updateSecondTitle(SecondTitle secondTitle){
        if("".equals(secondTitle.getSeTitle()))
            secondTitle.setSeTitle(null);
        return secondTitleService.updateSecondTitleBySid(secondTitle);
    }

    /**
     * 分页查询 描述信息
     */
    @ResponseBody
    @RequestMapping("querydesinfobypage")
    public List<DesInfo> queryDesInfosbyPage(PageInfo pageInfo){
        return desInfoService.queryDesInfoByPage(pageInfo);
    }

    /**
     * 查询 描述信息 一共有多少条 数据
     */
    @ResponseBody
    @RequestMapping("queryDesInfosNum")
    public Integer queryDesInfosNum(){
        return desInfoService.queryDesInfoNum();
    }

    /**
     * 修改mid,desInfo或者同时修改
     */
    @ResponseBody
    @RequestMapping("updateDesInfo")
    public Integer updateDesInfo(DesInfo desInfo){
        if("".equals(desInfo.getDeInfo()))
            desInfo.setDeInfo(null);
        return desInfoService.updateDesInfo(desInfo);
    }

    /**
     * 分页查询图片信息
     */
    @ResponseBody
    @RequestMapping("queryPictures")
    public List<Picture> queryPictures(PageInfo pageInfo){
        return pictureService.queryPictureByPage(pageInfo);
    }

    /**
     * 修改 图片所属 mid
     */
    @ResponseBody
    @RequestMapping("updatePictureInfo")
    public Integer updatePictureInfo(Picture picture){
        return pictureService.updatePicture(picture);
    }

    /**
     * 查找图片数
     */
    @ResponseBody
    @RequestMapping("queryPictureNum")
    public Integer queryPictureNum(){
        return pictureService.queryPictureNum();
    }

    /**
     * 增加描述信息
     */
    @ResponseBody
    @RequestMapping("adddesinfo")
    public Integer addDesInfo(DesInfo desInfo){
        return desInfoService.addDesInfo(desInfo);
    }

    /**
     * 上传图片
     */
    @RequestMapping("addpicture")
    public String addPicture(@RequestParam("file") MultipartFile file , @RequestParam("mid") Integer mid) throws IOException {
        String staticPath = System.getProperty("user.dir")+"\\src\\main\\resources\\static\\resources\\picture\\";
        //获取将上传的文件输入到 服务器内存中 的一个输入流
        InputStream inputStream = file.getInputStream();
        // 设置 上传文件的 文件名
        String fileName = "picture"+ (pictureService.queryMaxPicturePid()+1);
        //获取一个将 服务器内存中数据输出到服务器本地硬盘中的 输出流
        FileOutputStream fileOutputStream = new FileOutputStream(staticPath + fileName+".png");

        //具体流操作
        //缓冲区1M
        byte[] bytes = new byte[1024*1024];
        int len = -1;
        while (( len = inputStream.read(bytes) ) != -1){
            //代表从 缓冲区bytes 中的 0下标 开始写出 长度为len 的数据
            fileOutputStream.write(bytes,0,len);
            //将流中的数据刷 到 硬盘中;
            fileOutputStream.flush();
        }
        //关闭流
        fileOutputStream.close();
        inputStream.close();

        //将该图片名 保存在数据库中
        picture.setPicName(fileName);
        picture.setMid(mid);
        pictureService.addPicture(picture);

        return "redirect:/manage/picture.html";
    }

    /**
     * 增加主标题
     */
    @ResponseBody
    @RequestMapping("addmaintitle")
    public Integer addMainTitle(MainTitle mainTitle){
        return mainTitleService.addMianTilte(mainTitle);
    }

    /**
     * 增加次标题
     */
    @ResponseBody
    @RequestMapping("addsecondtitle")
    public Integer addSecondTitle(SecondTitle secondTitle){
        return secondTitleService.addSecondTitle(secondTitle);
    }

    /**
     * 删除 图片
     */
    @ResponseBody
    @RequestMapping("deletepicture")
    public Integer deletePicture(Integer pid){
        return pictureService.deletePicture(pid);
    }

    /**
     * 删除 主标题
     */
    @ResponseBody
    @RequestMapping("deletemaintitle")
    public Integer deleteMainTitle(Integer mid){
        return mainTitleService.deleteMainTitle(mid);
    }

    /**
     * 删除 次标题
     */
    @ResponseBody
    @RequestMapping("deletesecondtitle")
    public Integer deleteSecondTitle(Integer sid){
        return secondTitleService.deleteSecondTitle(sid);
    }

    /**
     * 删除 描述信息
     */
    @ResponseBody
    @RequestMapping("deletedesinfo")
    public Integer deleteDesInfo(int did){
        return desInfoService.deleteDesInfo(did);
    }
}
